package io.plus.user_interface.io.common.listener;

import drds.common.$;
import io.plus.interactive.AsynchronousSocketChannelContext;
import io.plus.user_interface.io.common.Constants;
import io.plus.user_interface.io.common.SessionContext;
import io.plus.user_interface.io.common.UserConfiguration;
import io.plus.user_interface.io.common.packet.Client;
import io.plus.user_interface.io.common.packet.Group;
import io.plus.user_interface.io.common.packet.User;
import io.plus.user_interface.io.common.utils.ImKit;
import io.plus.user_interface.redis.CacheImpl;
import io.plus.user_interface.redis.CacheManager;

import java.io.Serializable;
import java.util.List;

//import org.apache.commons.lang3.StringUtils;

/**
 * 消息持久化绑定监听器
 */
public class BindGroupListenerImpl implements BindGroupListener {
    static {
        CacheManager.register(Constants.user, Integer.MAX_VALUE, Integer.MAX_VALUE);
        CacheManager.register(Constants.group, Integer.MAX_VALUE, Integer.MAX_VALUE);
        CacheManager.register(Constants.store, Integer.MAX_VALUE, Integer.MAX_VALUE);
        CacheManager.register(Constants.push, Integer.MAX_VALUE, Integer.MAX_VALUE);

    }


    protected UserConfiguration userConfiguration;
    private CacheImpl group = null;
    private CacheImpl user = null;


    public BindGroupListenerImpl(UserConfiguration userConfiguration) {
        this.userConfiguration = userConfiguration;
        group = CacheManager.getCache(Constants.group);
        user = CacheManager.getCache(Constants.user);
    }

    public UserConfiguration getUserConfiguration() {
        return userConfiguration;
    }

    public void setUserConfiguration(UserConfiguration userConfiguration) {
        this.userConfiguration = userConfiguration;
    }

    /**
     * 初始化群组用户;
     */
    public void initUserGroupBindInfo(String groupId, AsynchronousSocketChannelContext asynchronousSocketChannelContext) {

        String userId = asynchronousSocketChannelContext.userId;
        if ($.isNullOrEmpty(groupId) || $.isNullOrEmpty(userId)) {
            return;
        }
        String groupIdWithUserKey = groupId + Constants.SUBFIX + Constants.user;
        List<String> userIdList = group.listGetAll(groupIdWithUserKey);
        if (!userIdList.contains(userId)) {
            group.listPushTail(groupIdWithUserKey, userId);
        }
        addGroupidToUserGroupIdList(userId, groupId);

        SessionContext sessionContext = (SessionContext) asynchronousSocketChannelContext.getAttribute();
        Client client = sessionContext.getClient();
        if (client == null) {
            return;
        }
        User onlineUser = client.getUser();
        if (onlineUser == null) {
            return;
        }
        List<Group> groups = onlineUser.getGroups();
        if (groups == null) {
            return;
        }
        for (Group group : groups) {
            if (groupId.equals(group.getId())) {
                this.group.put(groupId + Constants.SUBFIX + Constants.INFO, group);
                break;
            }
        }
    }

    /**
     * 初始化用户拥有哪些群组;
     */
    public void addGroupidToUserGroupIdList(String userid, String group) {

        if ($.isNullOrEmpty(group) || $.isNullOrEmpty(userid)) {
            return;
        }
        List<String> groups = user.listGetAll(userid + Constants.SUBFIX + Constants.group);
        if (!groups.contains(group)) {
            user.listPushTail(userid + Constants.SUBFIX + Constants.group, group);
        }
    }


    public void stroeUserInfo(User user) {

        String userId = user.getId();
        if ($.isNullOrEmpty(userId)) {
            return;
        }
        User userCopy = ImKit.copyUserWithoutFriendsGroups(user);
        this.user.put(userId + Constants.SUBFIX + Constants.INFO, userCopy);
        List<Group> friends = user.getFriends();
        if (friends != null) {
            this.user.put(userId + Constants.SUBFIX + Constants.FRIENDS, (Serializable) friends);
        }
    }

    /**
     * 初始化用户终端协议类型;
     *
     * @param status(online、offline)
     */
    @Override
    public void stroeUserStatus(AsynchronousSocketChannelContext asynchronousSocketChannelContext, String status) {

        String userId = asynchronousSocketChannelContext.userId;
        if ($.isNullOrEmpty(userId)) {
            return;
        }

        user.put(userId + Constants.SUBFIX + Constants.TERMINAL, status);
    }


    @Override
    public void onAfterGroupBind(AsynchronousSocketChannelContext asynchronousSocketChannelContext, String group) throws Exception {

        initUserGroupBindInfo(group, asynchronousSocketChannelContext);
    }

    @Override
    public void onAfterGroupUnbind(AsynchronousSocketChannelContext asynchronousSocketChannelContext, String groupId) throws Exception {
        String userId = asynchronousSocketChannelContext.userId;
        //移除群组成员;
        group.listRemove(groupId + Constants.SUBFIX + Constants.user, userId);
        //移除成员群组;
        user.listRemove(userId + Constants.SUBFIX + Constants.group, groupId);

        CacheManager.getCache(Constants.push).remove(Constants.group + Constants.SUBFIX + groupId + Constants.SUBFIX + userId);
    }

    @Override
    public void onAfterUserBind(AsynchronousSocketChannelContext asynchronousSocketChannelContext, String userid) throws Exception {

        SessionContext sessionContext = (SessionContext) asynchronousSocketChannelContext.getAttribute();
        Client client = sessionContext.getClient();
        if (client == null) {
            return;
        }
        User onlineUser = client.getUser();
        if (onlineUser != null) {
            stroeUserStatus(asynchronousSocketChannelContext, Constants.ONLINE);
            stroeUserInfo(onlineUser);
        }
    }

    @Override
    public void onAfterUserUnbind(AsynchronousSocketChannelContext asynchronousSocketChannelContext, String userid) throws Exception {

    }
}
